home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / NeuroSim 1.0 / .cp / CNeuroSimApp.cp < prev    next >
Text File  |  1996-02-19  |  9KB  |  325 lines

  1. // ===========================================================================
  2. //    CNeuroSimApp.cp                ©1996 Timo Eloranta
  3. // ===========================================================================
  4. //    An application class derived from the PowerPlant LApplication class.
  5. //    This class handles the menu commands and owns the neural net and the
  6. //    window inside of which our pane object is.
  7.  
  8. #include "CNeuroSimApp.h"
  9.  
  10. #include <LApplication.h>        //
  11. #include <LEditField.h>            //
  12. #include <LCaption.h>            //
  13. #include <LDialogBox.h>            //
  14. #include <LGroupBox.h>            // PowerPlant classes
  15. #include <LGrowZone.h>            //
  16. #include <LPicture.h>            //
  17. #include <LStdControl.h>        //
  18. #include <LTabGroup.h>            //
  19. #include <LWindow.h>            //
  20.  
  21. #include <UDesktop.h>            // Deactivate & Activate
  22. #include <UDrawingState.h>        // UQDGlobals
  23. #include <UModalDialogs.h>        // StDialogHandler
  24. #include <UMemoryMgr.h>            // InitializeHeap
  25. #include <URegistrar.h>            // RegisterClass
  26.  
  27. #include <PP_Messages.h>        // cmd_New, msg_OK
  28.  
  29. #include "CStdNeuralNet.h"        // The only net type we have implemented
  30.  
  31. #include "CNeuroSimPane.h"        // Our pane class
  32. #include "CParamsDialog.h"        // Our dialog class
  33.  
  34. #include "CMyCaption.h"            // My own caption class
  35. #include "Slider.h"                // Scott Squires' slider class
  36.  
  37. // ===========================================================================
  38. //        • Main Program
  39. // ===========================================================================
  40. //    A typical main function of a PowerPlant based Mac application
  41.  
  42. void main()
  43. {
  44.                                     // Set Debugging options
  45. #ifdef Debug_Throw
  46.     SetDebugThrow_(debugAction_Alert);
  47. #endif
  48. #ifdef Debug_Signal
  49.     SetDebugSignal_(debugAction_Alert);
  50. #endif
  51.  
  52.     InitializeHeap(3);                // Initialize Memory Manager
  53.     
  54.                                     // Initialize standard Toolbox managers
  55.     UQDGlobals::InitializeToolbox( &qd );
  56.     
  57.                                     // Initialize randomizer seed
  58.     ::GetDateTime((unsigned long *)&qd.randSeed);
  59.     
  60.     new LGrowZone(20000);            // Install a GrowZone function to catch
  61.                                     //  low memory situations.
  62.     
  63.     CNeuroSimApp    theApp;            // Create instance of our application
  64.     theApp.Run();                    //  class and run it
  65. }
  66.  
  67.  
  68. // ===========================================================================
  69. //        • CNeuroSimApp Class
  70. // ===========================================================================
  71.  
  72. // ---------------------------------------------------------------------------
  73. //        • CNeuroSimApp
  74. //
  75. //          Called by:    main()
  76. // ---------------------------------------------------------------------------
  77. //    Constructor
  78.  
  79. CNeuroSimApp::CNeuroSimApp()
  80. {
  81.     RegisterClasses();
  82.  
  83.         // Initialize parameters with default values
  84.         // (the defaults are defined in NS_Types.h).
  85.  
  86.     SGenParams theTemp    
  87.         = { DEFAULT_LENGTH_X_AVG,    DEFAULT_LENGTH_Y_AVG,
  88.             DEFAULT_LENGTH_X_DEV,    DEFAULT_LENGTH_Y_DEV,
  89.             DEFAULT_NET_SIZE,
  90.             DEFAULT_QTY_MIN,        DEFAULT_QTY_MAX };
  91.  
  92.     mParams = theTemp;
  93.     
  94.     mNet    = NULL;
  95.     mWindow = NULL;
  96. }
  97.  
  98.  
  99. // ---------------------------------------------------------------------------
  100. //        • ~CNeuroSimApp
  101. // ---------------------------------------------------------------------------
  102. //    Destructor
  103.  
  104. CNeuroSimApp::~CNeuroSimApp()
  105. {
  106.     if ( mWindow )    delete mWindow;
  107.     if ( mNet )        delete mNet;
  108. }
  109.  
  110.  
  111. // ---------------------------------------------------------------------------
  112. //        • ObeyCommand
  113. //
  114. //          Called by:    LCommander::ProcessCommand
  115. // ---------------------------------------------------------------------------
  116. //    Respond to commands
  117.  
  118. Boolean
  119. CNeuroSimApp::ObeyCommand(
  120.     CommandT    inCommand,
  121.     void        *ioParam)
  122. {
  123.     Boolean            cmdHandled = true;
  124.     CParamsDialog * theDialog;
  125.     
  126.     switch (inCommand) {
  127.     
  128.             // cmd_New, cmd_Params and cmd_Demo are menu commands
  129.     
  130.         case cmd_New:
  131.             InitNewNet();
  132.             break;
  133.  
  134.         case cmd_Params:
  135.             theDialog = (CParamsDialog *)
  136.                 LWindow::CreateWindow( WIND_Params, this);
  137.                 
  138.             theDialog -> InitDialog();
  139.             theDialog -> SetValues( mParams );
  140.             theDialog -> Show();
  141.             break;
  142.  
  143.         case cmd_Demo:
  144.             if ( mNet )
  145.                 mNet -> ChangeDemoMode();
  146.             break;
  147.  
  148.             // cmd_SetParams is the command which is sent by 
  149.             // the parameters dialog if the user presses "OK".
  150.             // We respond to it by reading in the new parameter
  151.             // values before throwing the dialog away.
  152.  
  153.         case cmd_SetParams:
  154.             theDialog = (CParamsDialog *) 
  155.                 ((SDialogResponse *) ioParam) -> dialogBox;
  156.                 
  157.             theDialog -> GetValues( mParams );
  158.             delete theDialog;
  159.             break;
  160.     
  161.             // By default we pass the command to our base class
  162.             // which handles cmd_About and cmd_Quit
  163.     
  164.         default:
  165.             cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
  166.             break;
  167.     }
  168.     
  169.     return cmdHandled;
  170. }
  171.  
  172. // ---------------------------------------------------------------------------
  173. //        • FindCommandStatus
  174. //
  175. //          Called by:    LCommander::ProcessCommandStatus
  176. // ---------------------------------------------------------------------------
  177. //    Pass back whether a Command is enabled and/or marked (in a Menu)
  178.  
  179. void
  180. CNeuroSimApp::FindCommandStatus(
  181.     CommandT    inCommand,
  182.     Boolean        &outEnabled,
  183.     Boolean        &outUsesMark,
  184.     Char16        &outMark,
  185.     Str255        outName)
  186. {
  187.     outUsesMark = false;
  188.  
  189.     switch (inCommand) {
  190.  
  191.         case cmd_New:
  192.         case cmd_Params:
  193.             outEnabled = true;
  194.             break;
  195.  
  196.             // The text of the "demo mode" menu item is set dynamically
  197.             // to "Start Demo" or "Stop Demo" depending on whether the
  198.             // mode is currently ON or OFF.
  199.  
  200.         case cmd_Demo:
  201.             if (mNet) {
  202.                 outEnabled = true;
  203.                 if (mNet -> DemoModeOn())
  204.                     ::GetIndString( outName, STRx_Menus, str_StopDemo );
  205.                 else
  206.                     ::GetIndString( outName, STRx_Menus, str_StartDemo );
  207.             } else
  208.                 outEnabled = false;
  209.             break;
  210.     
  211.             // We let our base class handle all the other commands
  212.     
  213.         default:
  214.             LApplication::FindCommandStatus(inCommand, outEnabled, 
  215.                                 outUsesMark, outMark, outName);
  216.             break;
  217.     }
  218. }
  219.  
  220. // ---------------------------------------------------------------------------
  221. //        • InitNewNet
  222. //
  223. //          Called by:    CNeuroSimApp::ObeyCommand
  224. // ---------------------------------------------------------------------------
  225. //    Create a new neural net and the window to display it in.
  226.  
  227. void
  228. CNeuroSimApp::InitNewNet()
  229. {
  230.     CNeuroSimPane *    thePane;
  231.  
  232.         // Throw out the old stuff if any
  233.  
  234.     if ( mWindow )    delete mWindow;
  235.     if ( mNet )        delete mNet;
  236.         
  237.         // Create and initialize a new net object.
  238.         
  239.     mNet = new CStdNeuralNet( mParams );
  240.  
  241.         // Create the main window from a resource
  242.         // (our pane is inside the window)
  243.         
  244.     mWindow    = LWindow::CreateWindow( WIND_NeuroSim, this );
  245.     thePane    = (CNeuroSimPane *) mWindow -> FindPaneByID( pane_NeuralNet );
  246.  
  247.         // "Introduce" the net and the pane to each other.
  248.  
  249.     thePane    -> SetNet( mNet );
  250.     mNet    -> SetPane( thePane );
  251.     
  252.     mWindow -> Show();
  253. }
  254.  
  255. // ---------------------------------------------------------------------------
  256. //        • ShowAboutBox
  257. //
  258. //          Called by:    LApplication::ObeyCommand
  259. // ---------------------------------------------------------------------------
  260. //    Display the About Box for NeuroSim
  261.  
  262. void
  263. CNeuroSimApp::ShowAboutBox()
  264. {
  265.     StDialogHandler        theHandler( WIND_NeuroSimAbout, this );
  266.     LWindow                *theDialog = theHandler.GetDialog();
  267.  
  268.     if ( theDialog ) {
  269.         while ( true ) {
  270.             MessageT    hitMessage = theHandler.DoDialog();
  271.             
  272.             if ( hitMessage == msg_OK )
  273.                 break;
  274.         }
  275.     } else {                            // This is only for the case where
  276.         UDesktop::Deactivate();            // we don't have enough memory
  277.         ::Alert( ALRT_About, nil );        // to show the finer About Box
  278.         UDesktop::Activate();            // requested above...
  279.     }
  280. }
  281.  
  282. // ---------------------------------------------------------------------------
  283. //        • RegisterClasses
  284. //
  285. //          Called by:    CNeuroSimApp::CNeuroSimApp
  286. // ---------------------------------------------------------------------------
  287. //    Register classes for objects created from 'PPob' resources
  288.  
  289. void
  290. CNeuroSimApp::RegisterClasses()
  291. {
  292.                 // --- First the PowerPlant classes --- //
  293.  
  294.     URegistrar::RegisterClass(    LCaption    ::class_ID,        
  295.                                 LCaption    ::CreateCaptionStream);
  296.     URegistrar::RegisterClass(    LDialogBox    ::class_ID,
  297.                                 LDialogBox    ::CreateDialogBoxStream);
  298.     URegistrar::RegisterClass(    LEditField    ::class_ID,
  299.                                 LEditField    ::CreateEditFieldStream);
  300.     URegistrar::RegisterClass(    LGroupBox    ::class_ID,
  301.                                 LGroupBox    ::CreateGroupBoxStream);
  302.     URegistrar::RegisterClass(    LPane        ::class_ID,
  303.                                 LPane        ::CreatePaneStream);
  304.     URegistrar::RegisterClass(    LPicture    ::class_ID,
  305.                                 LPicture    ::CreatePictureStream);
  306.     URegistrar::RegisterClass(    LStdButton    ::class_ID,
  307.                                 LStdButton    ::CreateStdButtonStream);
  308.     URegistrar::RegisterClass(    LTabGroup    ::class_ID,
  309.                                 LTabGroup    ::CreateTabGroupStream);
  310.     URegistrar::RegisterClass(    LView        ::class_ID,
  311.                                 LView        ::CreateViewStream);
  312.     URegistrar::RegisterClass(    LWindow        ::class_ID,
  313.                                 LWindow        ::CreateWindowStream);
  314.  
  315.                 // --- Then the non-PowerPlant classes --- //
  316.  
  317.     URegistrar::RegisterClass(    HorzSlider        ::class_ID,
  318.                                 HorzSlider        ::CreateHorzSliderStream);
  319.     URegistrar::RegisterClass(    CNeuroSimPane    ::class_ID,
  320.                                 CNeuroSimPane    ::CreateNeuroSimPaneStream);
  321.     URegistrar::RegisterClass(    CParamsDialog    ::class_ID,
  322.                                 CParamsDialog    ::CreateParamsDialogStream);
  323.     URegistrar::RegisterClass(    CMyCaption        ::class_ID,
  324.                                 CMyCaption        ::CreateMyCaptionStream);
  325. }